package com.chance.cc.crawler.development.bootstrap.xcar;

import com.chance.cc.crawler.core.selector.Json;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author bx
 * @date 2021/2/4 0004 16:45
 */
public class XcarDataStatic {

    public static void main(String[] args) throws IOException {
        statics();
    }

    public static void test() throws IOException {
        List<String> lines = IOUtils.readLines(new FileInputStream("C:\\Users\\Administrator\\Desktop\\jar\\xcar.json"), "utf-8");
        List<String> ting_lines = IOUtils.readLines(new FileInputStream("C:\\Users\\Administrator\\Desktop\\jar\\xcar_ting.txt"), "utf-8");
        List<String> results = new ArrayList<>();
        for (String line : lines) {
            Json lineJson = new Json(line);
            Long releaseTime = Long.valueOf(lineJson.jsonPath("$.releaseTime").get());
            String dataId = lineJson.jsonPath("$.dataId").get();

            if (releaseTime >= 1612281600000l && releaseTime <= 1612368000000L){
                results.add(dataId);
            }
        }
        System.out.println(results.size());
        results.removeAll(ting_lines);
        System.out.println(results.size());
        IOUtils.writeLines(results,"\n",new FileWriter("C:\\Users\\Administrator\\Desktop\\jar\\xcar_results.json"));

    }

    public static void statics() throws IOException {
        List<String> lines = IOUtils.readLines(new FileInputStream("C:\\Users\\Administrator\\Desktop\\jar\\vm_auto_comment_05.log"), "utf-8");
        commentStatics(lines,1612368000000l,1612454400000L,"xcar","news","news");
        commentStatics(lines,1612368000000l,1612454400000L,"xcar","news","video");
        commentStatics(lines,1612368000000l,1612454400000L,"xcar","forum");
        commentStatics(lines,0,System.currentTimeMillis(),"xcar","publicPraise");

    }

    private static void  commentStatics(List<String> lines,long startTime,long endTime,String... dataIdPrefix){
        String prefix = StringUtils.joinWith("-", dataIdPrefix);
        int articleCount =0;
        int replyCount =0;
        int interactionCount =0;
        int allCount =0;
        for (String line : lines) {
            Json lineJson = new Json(line);
            Long releaseTime = Long.valueOf(lineJson.jsonPath("$.releaseTime").get());
            String dataId = lineJson.jsonPath("$.dataId").get();

            if (releaseTime > startTime && releaseTime <= endTime){
                if (dataId.startsWith(prefix)){
                    allCount++;
                    if (dataId.startsWith(StringUtils.joinWith("-",prefix,"article"))){
                        articleCount++;
                    }
                    if (dataId.startsWith(StringUtils.joinWith("-",prefix,"comment"))){
                        replyCount++;
                    }
                    if (dataId.startsWith(StringUtils.joinWith("-",prefix,"interaction"))){
                        interactionCount++;
                    }
                }

            }
        }
        System.out.println(StringUtils.joinWith(" ",dataIdPrefix) + " article count:"+articleCount);
        System.out.println(StringUtils.joinWith(" ",dataIdPrefix) + " comment count:"+replyCount);
        System.out.println(StringUtils.joinWith(" ",dataIdPrefix) + " interaction count:"+interactionCount);
        System.out.println(StringUtils.joinWith(" ",dataIdPrefix) + " all count:"+allCount);
    }
}
